package com.zz.wechatshop.service.impl;

import com.zz.common.utils.StringUtils;
import com.zz.shop.dao.ShopCarouselDao;
import com.zz.shop.domain.ShopProductCategoryDO;
import com.zz.shopAdmin.service.AdminShopProductCategoryService;
import com.zz.wechatshop.service.CategoryService;
import com.zz.wechatshop.vo.WechatCategoryVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class CategoryServiceImpl implements CategoryService {
  @Autowired
  AdminShopProductCategoryService adminShopProductCategoryService;
  @Autowired
  ShopCarouselDao carouselDao;

  static String rootpid="";

  @Value("${ImageHost}")
  String imageHost;


  @Override
  public List<WechatCategoryVO> getAllCategoryList() {
    return adminShopProductCategoryService.list(null).stream().map((item) -> {
      WechatCategoryVO re= package_to_WechatCategoryVO(item);
      String icon = re.getIcon();
      re.setIcon(imageHost + icon);
      return re;
    }).collect(Collectors.toList());
  }

  @Override
  public WechatCategoryVO get(String id) {
    WechatCategoryVO re= package_to_WechatCategoryVO(adminShopProductCategoryService.get(id));
    String icon = re.getIcon();
    re.setIcon(imageHost + icon);
    return re;
  }


  public  WechatCategoryVO package_to_WechatCategoryVO(ShopProductCategoryDO pacDo){
    WechatCategoryVO reVo=  new WechatCategoryVO();
    reVo.setId(pacDo.getId());
    reVo.setPaixu(pacDo.getSort()==null?0:pacDo.getSort());

    if(StringUtils.isBlank(pacDo.getPid())){
      reVo.setLevel(1);
      reVo.setPid(rootpid);
    }else{
      reVo.setLevel(2);
      reVo.setPid(pacDo.getPid());
    }
    reVo.setKey(pacDo.getName());
    reVo.setName(pacDo.getName());
    reVo.setIcon(pacDo.getIcon());
    reVo.setUse(pacDo.isActive());

    //---暂
    reVo.setShopId(pacDo.getCreatedBy());
    reVo.setUserId(pacDo.getCreatedBy());
    //---
    return reVo;
  }

}
